import turtle
import time
import math
import threading
from datetime import datetime

class PomodoroTimer:
    def __init__(self):
        # 设置中文字体支持
        self.font = ("SimHei", 16, "normal")
        self.large_font = ("SimHei", 24, "bold")
        
        # 计时器状态
        self.is_running = False
        self.is_work_mode = True
        self.work_minutes = 25
        self.rest_minutes = 5
        self.remaining_seconds = self.work_minutes * 60
        
        # 创建主窗口
        self.screen = turtle.Screen()
        self.screen.title("番茄工作法计时器")
        self.screen.setup(width=600, height=500)
        self.screen.bgcolor("#f0f0f0")
        self.screen.tracer(0)  # 关闭自动刷新
        
        # 创建显示 turtle
        self.display_turtle = turtle.Turtle()
        self.display_turtle.hideturtle()
        self.display_turtle.penup()
        self.display_turtle.goto(0, 100)
        
        # 创建状态 turtle
        self.status_turtle = turtle.Turtle()
        self.status_turtle.hideturtle()
        self.status_turtle.penup()
        self.status_turtle.goto(0, 0)
        
        # 创建动画 turtle
        self.anim_turtle = turtle.Turtle()
        self.anim_turtle.hideturtle()
        self.anim_turtle.penup()
        
        # 创建按钮区域
        self.create_buttons()
        
        # 更新显示
        self.update_display()
        
        # 绑定按键事件
        self.screen.listen()
        self.screen.onkeypress(self.toggle_timer, "space")
        self.screen.onkeypress(self.reset_timer, "r")
        self.screen.onkeypress(self.switch_mode, "m")
        
        # 显示提示
        self.info_turtle = turtle.Turtle()
        self.info_turtle.hideturtle()
        self.info_turtle.penup()
        self.info_turtle.goto(0, -200)
        self.info_turtle.write("空格: 开始/暂停 | R: 重置 | M: 切换模式", 
                              align="center", font=self.font)
        
        # 启动主循环
        self.screen.mainloop()
    
    def create_buttons(self):
        """创建按钮区域"""
        self.button_turtle = turtle.Turtle()
        self.button_turtle.hideturtle()
        self.button_turtle.penup()
        self.button_turtle.goto(0, -100)
        
        # 绘制按钮
        self.draw_button(-100, -100, "开始", "#4CAF50")
        self.draw_button(0, -100, "重置", "#f44336")
        self.draw_button(100, -100, "切换", "#2196F3")
    
    def draw_button(self, x, y, text, color):
        """绘制一个按钮"""
        self.button_turtle.goto(x - 50, y - 20)
        self.button_turtle.pendown()
        self.button_turtle.color(color)
        self.button_turtle.begin_fill()
        for _ in range(2):
            self.button_turtle.forward(100)
            self.button_turtle.left(90)
            self.button_turtle.forward(40)
            self.button_turtle.left(90)
        self.button_turtle.end_fill()
        self.button_turtle.penup()
        
        # 绘制按钮文字
        self.button_turtle.goto(x, y - 10)
        self.button_turtle.color("white")
        self.button_turtle.write(text, align="center", font=self.font)
    
    def format_time(self, seconds):
        """格式化时间为 MM:SS 格式"""
        mins = seconds // 60
        secs = seconds % 60
        return f"{mins:02d}:{secs:02d}"
    
    def update_display(self):
        """更新显示"""
        # 清除之前的内容
        self.display_turtle.clear()
        self.status_turtle.clear()
        
        # 显示时间
        self.display_turtle.write(self.format_time(self.remaining_seconds), 
                                 align="center", font=self.large_font)
        
        # 显示状态
        status = "工作时间" if self.is_work_mode else "休息时间"
        status_color = "red" if self.is_work_mode else "green"
        
        self.status_turtle.color(status_color)
        self.status_turtle.write(status, align="center", font=self.font)
        
        # 更新按钮文字
        self.button_turtle.clear()
        self.create_buttons()
        
        # 刷新屏幕
        self.screen.update()
    
    def toggle_timer(self):
        """切换计时器状态（开始/暂停）"""
        self.is_running = not self.is_running
        
        if self.is_running:
            self.start_timer()
    
    def start_timer(self):
        """启动计时器"""
        if self.is_running and self.remaining_seconds > 0:
            self.remaining_seconds -= 1
            self.update_display()
            self.screen.ontimer(self.start_timer, 1000)
        elif self.remaining_seconds <= 0:
            # 计时结束，显示动画
            self.is_running = False
            if self.is_work_mode:
                self.show_tomato_animation()
            else:
                self.show_leaf_animation()
            # 切换模式
            self.switch_mode()
            # 重置时间
            self.reset_timer()
            # 自动开始下一轮
            self.toggle_timer()
    
    def reset_timer(self):
        """重置计时器"""
        self.is_running = False
        if self.is_work_mode:
            self.remaining_seconds = self.work_minutes * 60
        else:
            self.remaining_seconds = self.rest_minutes * 60
        self.update_display()
    
    def switch_mode(self):
        """切换工作/休息模式"""
        self.is_work_mode = not self.is_work_mode
        self.reset_timer()
    
    def draw_tomato(self, size, color):
        """绘制番茄"""
        self.anim_turtle.clear()
        self.anim_turtle.penup()
        self.anim_turtle.goto(0, -size)
        self.anim_turtle.color(color)
        self.anim_turtle.begin_fill()
        self.anim_turtle.circle(size)
        self.anim_turtle.end_fill()
        
        # 绘制番茄顶部
        self.anim_turtle.penup()
        self.anim_turtle.goto(0, size)
        self.anim_turtle.color("green")
        self.anim_turtle.begin_fill()
        self.anim_turtle.circle(size * 0.2)
        self.anim_turtle.end_fill()
        
        # 绘制番茄纹理
        self.anim_turtle.penup()
        self.anim_turtle.goto(-size, 0)
        self.anim_turtle.pendown()
        self.anim_turtle.color("#cc0000")
        self.anim_turtle.width(2)
        for i in range(4):
            angle = 30 + i * 30
            self.anim_turtle.setheading(angle)
            self.anim_turtle.forward(size * 0.8)
            self.anim_turtle.backward(size * 0.8)
        
        self.screen.update()
    
    def show_tomato_animation(self):
        """显示番茄动画"""
        # 清除主显示
        self.display_turtle.clear()
        self.status_turtle.clear()
        
        # 生长动画
        for size in range(10, 100, 2):
            color = (min(1.0, size/100), 0.2, 0.2)
            self.draw_tomato(size, color)
            time.sleep(0.05)
        
        # 脉动效果
        for i in range(10):
            size = 100 + (i % 2) * 5
            self.draw_tomato(size, "red")
            time.sleep(0.2)
        
        # 显示完成文本
        self.anim_turtle.clear()
        self.anim_turtle.penup()
        self.anim_turtle.goto(0, 0)
        self.anim_turtle.color("red")
        self.anim_turtle.write("完成一个番茄！", align="center", font=self.large_font)
        self.screen.update()
        time.sleep(2)
        self.anim_turtle.clear()
    
    def draw_leaf(self, x, y, size, angle):
        """绘制一片叶子"""
        self.anim_turtle.penup()
        self.anim_turtle.goto(x, y)
        self.anim_turtle.setheading(angle)
        self.anim_turtle.color("green")
        self.anim_turtle.begin_fill()
        self.anim_turtle.forward(size)
        self.anim_turtle.circle(size * 0.3, 180)
        self.anim_turtle.backward(size)
        self.anim_turtle.end_fill()
    
    def show_leaf_animation(self):
        """显示叶子动画"""
        # 清除主显示
        self.display_turtle.clear()
        self.status_turtle.clear()
        
        # 飘动的叶子
        leaves = []
        for _ in range(8):
            x = -250 + (_ * 70)
            y = 200
            size = 20 + (_ % 3) * 10
            angle = 0
            leaves.append((x, y, size, angle, _ * 0.05))
        
        for _ in range(50):
            self.anim_turtle.clear()
            for i in range(len(leaves)):
                x, y, size, angle, speed = leaves[i]
                y -= 5
                angle += speed
                leaves[i] = (x, y, size, angle, speed)
                self.draw_leaf(x, y, size, angle)
            self.screen.update()
            time.sleep(0.1)
        
        # 显示完成文本
        self.anim_turtle.clear()
        self.anim_turtle.penup()
        self.anim_turtle.goto(0, 0)
        self.anim_turtle.color("green")
        self.anim_turtle.write("休息结束！", align="center", font=self.large_font)
        self.screen.update()
        time.sleep(2)
        self.anim_turtle.clear()

if __name__ == "__main__":
    PomodoroTimer()
      